package com.wxtt.android.common;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.fast.dev.frame.BaseActivity;
import com.fast.dev.frame.utils.UIUtils;
import com.wxtt.android.R;
import com.wxtt.android.dialog.LoadingDialog;
import com.wxtt.android.dialog.TipDialog;
import com.wxtt.android.iterfaces.I_LoadingDialog;
import com.wxtt.android.iterfaces.I_TipDialog;
import com.wxtt.android.iterfaces.I_ToolbarNavigation;
import com.wxtt.android.view.ToolbarHelper;
import com.wxtt.android.view.ViewInject;

/**
 * 说明：业务Activity，所有Activity都继承该Activity
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2015/12/26 13:01
 * <p/>
 * 版本：verson 1.0
 */
public abstract class CommonActivity extends BaseActivity implements I_ToolbarNavigation,I_LoadingDialog,I_TipDialog{

    private ToolbarHelper toolbarHelper;
    private Toolbar toolbar;
    //等待框
    private LoadingDialog mLoadingDialog;
    //提示框
    private TipDialog mTipDialog;

    @Override
    public void onInit(Bundle bundle) {
        if (mLoadingDialog == null){
            mLoadingDialog = new LoadingDialog(this);
        }
        if (mTipDialog == null){
            mTipDialog = new TipDialog(this);
        }
    }

    @Override
    public void clickView(View v) {

    }

    @Override
    public int setRootView() {
        return 0;
    }

    /********************************显示TipDialog**************************************/

    /**
     * 说明：说明：显示提示框，点击空白消失，返回消失
     * @param msg
     * @param listener
     */
    @Override
    public void showTipDialog(String msg,TipDialog.TipClickListener listener){
        showTipDialog(getString(R.string.tip),msg,listener);
    }

    /**
     * 说明：说明：显示提示框，点击空白消失，返回消失
     * @param resId
     * @param listener
     */
    @Override
    public void showTipDialog(int resId,TipDialog.TipClickListener listener){
        showTipDialog(getString(resId),listener);
    }

    /**
     * 说明：显示提示框，点击空白消失，返回消失
     * @param tip
     * @param msg
     * @param listener
     */
    @Override
    public void showTipDialog(String tip,String msg,TipDialog.TipClickListener listener){
        showTipDialog(tip,msg,true,listener);
    }

    /**
     * 说明：显示提示框，点击空白消失，返回消失
     * @param tip
     * @param msg
     * @param listener
     */
    @Override
    public void showTipDialog(int tip,int msg,TipDialog.TipClickListener listener){
        showTipDialog(getString(tip),getString(msg),true,listener);
    }

    /**
     * 说明：显示提示框，点击空白消失，返回消失
     * @param tip
     * @param msg
     * @param isShowCancel
     * @param listener
     */
    @Override
    public void showTipDialog(String tip,String msg,boolean isShowCancel,TipDialog.TipClickListener listener){
        showTipDialog(tip,msg,isShowCancel,true,true,listener);
    }

    /**
     * 说明：显示提示框
     * @param tip 提示框标题
     * @param msg 提示框内容
     * @param isShowCancel 是否显示取消按钮，true显示，false不显示
     * @param outTouch 点击空白区域是否消失
     * @param cancel 点击返回键是否消失
     * @param listener 按键监听
     */
    @Override
    public void showTipDialog(String tip,String msg,boolean isShowCancel,boolean outTouch,boolean cancel,TipDialog.TipClickListener listener){
        if (mTipDialog == null){
            mTipDialog = new TipDialog(this);
        }
        dismisssTipDialog();
        mTipDialog.setTip(tip).setContent(msg);
        if (!isShowCancel){
            mTipDialog.hiddenCancel();
        }
        mTipDialog.setCancelable(cancel);
        mTipDialog.setCanceledOnTouchOutside(outTouch);
        mTipDialog.setClickListener(listener);
        mTipDialog.show();
    }

    /**
     * 说明：关闭提示框
     */
    @Override
    public void dismisssTipDialog() {
        if (mTipDialog != null && mTipDialog.isShowing()){
            mTipDialog.dismiss();
        }
    }

    /********************************显示TipDialog**************************************/

    /********************************显示LoadingDialog**************************************/

    /**
     * 说明：显示等待框，点击空白无法取消，返回可以取消
     */
    @Override
    public void showLoading() {
        showLoading(getString(R.string.str_loading),false,true);
    }

    /**
     * 说明：说明：显示等待框，点击空白无法取消，返回可以取消
     * @param msg 等待提示语
     */
    @Override
    public void showLoading(String msg) {
        showLoading(msg,false,true);
    }

    /**
     * 说明：说明：显示等待框，点击空白无法取消，返回可以取消
     * @param resId 等待提示语
     */
    @Override
    public void showLoading(int resId) {
        showLoading(resId,false,true);
    }

    /**
     * 说明：显示等待框，返回可以取消
     * @param msg
     * @param outTouch
     */
    @Override
    public void showLoading(String msg, boolean outTouch) {
        showLoading(msg,outTouch,true);
    }

    /**
     * 说明：显示等待框，返回可以取消
     * @param resId
     * @param outTouch
     */
    @Override
    public void showLoading(int resId, boolean outTouch) {
        showLoading(resId,outTouch,true);
    }

    /**
     * 说明：显示等待框
     * @param msg
     * @param outTouch
     * @param cancel
     */
    @Override
    public void showLoading(String msg, boolean outTouch, boolean cancel) {
        if (mLoadingDialog == null){
            mLoadingDialog = new LoadingDialog(this);
        }
        if (mLoadingDialog.isShowing()){
            mLoadingDialog.dismiss();
        }
        mLoadingDialog.setText(msg).setCancel(cancel).setCancelOnTouch(outTouch).show();
    }

    /**
     * 说明：显示等待框
     * @param resId
     * @param outTouch
     * @param cancel
     */
    @Override
    public void showLoading(int resId, boolean outTouch, boolean cancel) {
        if (mLoadingDialog == null){
            mLoadingDialog = new LoadingDialog(this);
        }
        if (mLoadingDialog.isShowing()){
            mLoadingDialog.dismiss();
        }
        mLoadingDialog.setText(resId).setCancel(cancel).setCancelOnTouch(outTouch).show();
    }

    /**
     * 说明：关闭等待框
     */
    @Override
    public void dismissLoading(){
        if (mLoadingDialog != null && mLoadingDialog.isShowing()){
            mLoadingDialog.dismiss();
        }
    }
    /********************************显示LoadingDialog*********************************************/

    /********************************显示Toast*********************************************/

    public void cancelToast(){
        ViewInject.get().cancelToast();
    }
    public void shortToast(String msg){ViewInject.get().shortToast(msg);}
    public void shortToast(int resId){
        ViewInject.get().shortToast(resId);
    }
    public void longToast(String msg){
        ViewInject.get().longToast(msg);
    }
    public void longToast(int resId){
        ViewInject.get().longToast(resId);
    }

    public void shortMessage(View view,String msg){
        ViewInject.get().shortSnackbar(view, msg, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white));
    }
    public void shortMessage(View view,int resId){
        ViewInject.get().shortSnackbar(view, getString(resId), UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white));
    }
    public void longMessage(View view,String msg){
        ViewInject.get().longSnackbar(view, msg, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white));
    }
    public void longMessage(View view,int resId){
        ViewInject.get().longSnackbar(view, getString(resId), UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white));
    }
    public void shortMessage(View view,String msg,String action,View.OnClickListener listener){
        ViewInject.get().shortSnackbar(view, msg, action, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white), listener);
    }
    public void shortMessage(View view,int resId,String action,View.OnClickListener listener){
        ViewInject.get().shortSnackbar(view, getString(resId), action, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white), listener);
    }
    public void longMessage(View view,String msg,String action,View.OnClickListener listener){
        ViewInject.get().longSnackbar(view, msg, action, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white), listener);
    }
    public void alwaysMessage(View view,String msg,String action,View.OnClickListener listener){
        ViewInject.get().alwaysSnackbar(view, msg, action, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white), listener);
    }
    public void longMessage(View view,int resId,String action,View.OnClickListener listener){
        ViewInject.get().longSnackbar(view, getString(resId), action, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white), listener);
    }
    public void alwaysMessage(View view,int resId,String action,View.OnClickListener listener){
        ViewInject.get().alwaysSnackbar(view, getString(resId), action, UIUtils.getColor(R.color.snackbar_color), UIUtils.getColor(R.color.white), listener);
    }
    public void dismissMessage(){
        ViewInject.get().dismissSnackbar();
    }

    /**********************************结束***********************************************/


    /********************************菜单设置*********************************************/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (setMenu() != 0){
            getMenuInflater().inflate(setMenu(), menu);
            return true;
        }else {
            return super.onCreateOptionsMenu(menu);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (setMenu() != 0){
            onMenuItemClick(item.getItemId());
            return true;
        }else {
            return super.onOptionsItemSelected(item);
        }
    }

    /**
     * 说明：菜单点击处理
     * @param id
     */
    protected void onMenuItemClick(int id){}

    /**
     * 说明：设置菜单选项
     * @return
     */
    protected int setMenu(){
        return 0;
    }

    /********************************菜单设置*********************************************/


    /**********************************配置Toolbar****************************************/

    /**
     * 说明：是否显示Toolbar
     * @return
     */
    protected boolean showToolbar(){
        return false;
    }

    @Override
    public void setContentView(int layoutResID) {
        if (showToolbar()){
            toolbarHelper = new ToolbarHelper(this,layoutResID);
            toolbar = toolbarHelper.getToolbar();
            setContentView(toolbarHelper.getView());
            CustomToolbar(toolbarHelper.getView(),toolbar);
            setSupportActionBar(toolbar);
            onCustomToolbar(toolbarHelper.getView(),toolbar);
        }else {
            super.setContentView(layoutResID);
        }
    }

    /**
     * 说明：自定义布局
     * @param view
     * @param toolbar
     */
    protected void onCustomToolbar(View view,Toolbar toolbar){}

    /**
     * 说明：自定义布局
     * @param view
     * @param toolbar
     */
    private void CustomToolbar(View view,Toolbar toolbar){
        toolbar.setTitleTextColor(UIUtils.getColor(R.color.white));
        toolbar.setContentInsetsRelative(0,0);
        if (setToolbarTitle() != 0){
            toolbar.setTitle(getString(setToolbarTitle()));
            toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white_24dp);
        }else {
            toolbar.setTitle(R.string.app_name);
        }
        if (setToolbarView() != 0){
            getLayoutInflater().inflate(setToolbarView(),toolbar);
        }
    }

    /**
     * 说明：设置自定义布局
     * @return
     */
    protected int setToolbarView(){
        return 0;
    }

    /**
     * 说明：设置标题
     * @return
     */
    @Override
    public int setToolbarTitle() {
        return 0;
    }

    /**********************************配置Toolbar****************************************/

}
